package cn.walls1717.ggkt.wechat.service.impl;

import cn.walls1717.ggkt.exception.MyException;
import cn.walls1717.ggkt.model.wechat.Menu;
import cn.walls1717.ggkt.vo.wechat.MenuVo;
import cn.walls1717.ggkt.wechat.mapper.MenuMapper;
import cn.walls1717.ggkt.wechat.service.MenuService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 订单明细服务实现类
 *
 * @author walls1717
 * @since 2022/07/19
 */
@Service
@Slf4j
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Autowired
    private WxMpService wxMpService;

    /*@Override
    public List<MenuVo> findMenuInfo() {
        // 所有菜单数据
        List<Menu> menuList = this.list();
        // 一级菜单数据
        List<Menu> menuOneInfo = this.findMenuOneInfo();
        // 将一级菜单数据封装为VO集合
        return menuOneInfo.stream().map(item -> {
            MenuVo menuVo = new MenuVo();
            BeanUtils.copyProperties(item, menuVo);
            Long id = item.getId();
            // 二级菜单数据
            List<Menu> twoMenu = menuList.stream().filter(menu -> menu.getParentId().equals(id)).collect(Collectors.toList());
            List<MenuVo> twoMenuVo = new ArrayList<>();
            for (Menu menu : twoMenu) {
                MenuVo menuVo1 = new MenuVo();
                BeanUtils.copyProperties(menu, menuVo1);
                twoMenuVo.add(menuVo1);
            }
            menuVo.setChildren(twoMenuVo);
            return menuVo;
        }).collect(Collectors.toList());
    }*/

    @Override
    public List<MenuVo> listMenus() {
        return baseMapper.findMenuInfo();
    }

    @Override
    public List<Menu> listOneMenus() {
        LambdaQueryWrapper<Menu> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Menu::getParentId, 0);
        return this.list(lqw);
    }

    @Override
    public void updateWechatMenu() {
        List<MenuVo> menuVoList = this.listMenus();
        JSONArray buttonList = new JSONArray();
        for (MenuVo oneMenuVo : menuVoList) {
            // 一级菜单
            JSONObject one = new JSONObject();
            one.put("name", oneMenuVo.getName());
            // 二级菜单
            JSONArray subButton = new JSONArray();
            for (MenuVo twoMenuVo : oneMenuVo.getChildren()) {
                JSONObject view = new JSONObject();
                view.put("type", twoMenuVo.getType());
                if ("view".equals(twoMenuVo.getType())) {
                    view.put("name", twoMenuVo.getName());
                    view.put("url", "http://front.free.idcfengye.com/#"
                            + twoMenuVo.getUrl());
                } else {
                    view.put("name", twoMenuVo.getName());
                    view.put("key", twoMenuVo.getMeunKey());
                }
                subButton.add(view);
            }
            one.put("sub_button", subButton);
            buttonList.add(one);
        }
        // 菜单
        JSONObject button = new JSONObject();
        button.put("button", buttonList);
        try {
            String menuId = wxMpService.getMenuService().menuCreate(button.toJSONString());
            log.info("menuId : {}",menuId);
        } catch (WxErrorException e) {
            e.printStackTrace();
            throw new MyException(20001, "微信公众号菜单同步失败");
        }
    }

    @Override
    public void deleteMenu() {
        try {
            wxMpService.getMenuService().menuDelete();
        } catch (WxErrorException e) {
            e.printStackTrace();
            throw new MyException(20001, "公众号菜单删除失败");
        }
    }
}
